package com.hcb.adapter;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hcb.adapter.ListviewAdapter.viewHolder;
import com.hcb.app911.R;
import com.hcb.common.ActionEvent;
import com.hcb.constants.Constants;
import com.hcb.controller.ContactController;
import com.hcb.dto.ContactsDTO;
import com.hcb.dto.ListViewDTO;

public class AdapterAddContacts extends ArrayAdapter<Object> {
	Context mContext;
	private ArrayList<Object> arrObject;
	Dialog dialog;
	Button btnyes, btnno;

	public AdapterAddContacts(Context context, int resource,
			ArrayList<Object> arr) {
		super(context, resource, arr);
		mContext = context;
		arrObject = arr;
	}

	class viewHolder {
		Button btnremove;
		TextView txtContacname;
		TextView txtContacnumber;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrObject.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return arrObject.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView;
		rowView = inflater.inflate(R.layout.adapter_emergency_contacts, parent,
				false);

		viewHolder holder = new viewHolder();

		// holder.icon = (ImageView) rowView.findViewById(R.id.icon);
		holder.txtContacname = (TextView) rowView
				.findViewById(R.id.txtContacname);
		holder.txtContacnumber = (TextView) rowView
				.findViewById(R.id.txtContacnumber);
		holder.btnremove = (Button) rowView.findViewById(R.id.btnremove);

		ContactsDTO dto = (ContactsDTO) arrObject.get(position);

		// holder.icon.setImageResource(dto.getImg());
		if (!dto.equals("")) {
			holder.txtContacname.setText(dto.getName());
			holder.txtContacnumber.setText(dto.getNumber());
		}

		holder.btnremove.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				dialog = new Dialog(getContext(),
						android.R.style.Theme_Translucent);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

				dialog.setCancelable(true);
				dialog.setContentView(R.layout.dialog_delete_contacts);
				btnyes = (Button) dialog.findViewById(R.id.btnyes);
				btnno = (Button) dialog.findViewById(R.id.btnno);

				btnyes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						ActionEvent e = new ActionEvent(mContext,
								Constants.DELETE_CONTACT, arrObject
										.get(position), null);
						ContactController.getInstance().handleViewEvent(e);
						arrObject.remove(position);
						notifyDataSetChanged();
						dialog.dismiss();
					}
				});
				btnno.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();

					}
				});

				dialog.show();

			}
		});

		return rowView;
	}

}